<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\admin\model\Node as NodeModel;
use think\Request;
use Qiniu\Auth as Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;

class Contract extends Admin
{
   //合同数据遍历到页面
    public function index()
    {
        $list = db('material_contract')->select();
        return view('admin@Contract/material',
            ['list'=>$list]
        );

    }
    
    public function showdetail($id){

    	$list = db('lab_contract')->where('id',$id)->find();

    	$contract= $list['contract_details'];

        $contract="./".$contract;

	    $content= file_get_contents("$contract");

		$this->assign("content",$content);

        return view('admin@/main/form',

            ['list'=>$content]);
    }
    
    
    //查询项目名称，遍历到页面，
    public function formMaterial()
    {
        $list = db('material_plan')->select();
        return view('admin@Contract/formMaterial',
            ['list'=>$list]
        );
    }
    //更新过的数据存到数据库
    public function addMaterial()
    {
        $data = $_POST;
        $result = db('material_contract')->insert($data);
        if ($result>0) {
            $info['status'] = true;
        }else{
            $info['status'] = false;
        }
        return  json($info);
    }

    // 将数据都遍历到页面上
    public function Labcontract()
    {
        $list = db('lab_contract')->paginate(7);
//        var_dump($list);exit;
        $this->assign('list', $list);

        return view('admin@Contract/labcontract',
            ['list'=>$list]
        );

    }
    //将合同遍历到页面上，查看详情
    public function detailsMaterial($id)
    {
        $list= db('material_contract')->where('id',$id)->find();
        return view('admin@Contract/detailsMaterial',
            ['list'=>$list]
        );
    }
    //将一条数据遍历到页面，可以进行更改
    public function editorMaterial($id)
    {
        $list= db('material_contract')->where('id',$id)->find();
        return view('admin@Contract/editorMaterial',
            ['list'=>$list]
        );
    }
    //将材料内容更新存到数组库
    public function updateMaterial($id)
    {
        $data = $_POST;

        $result = db('material_contract')->where('id',$id)->update($data);
        if ($result){
            $info['status'] = true;
        }else{
            $info['status'] =false;
        }

        return json($info);

    }
    //删除材料合同
    public function delectMaterial($id)
    {
        $result = db('material_contract')->where(['id'=>$id])->delete();

        if ($result) {
            $info['status'] = true;
        }else{
            $info['status'] = false;
        }
        return  json($info);
    }


    // 将一条劳务合同数据遍历到页面
    public function labcontractDetails($id)
    {
        $list = db('lab_contract')->where('id',$id)->find();
        return  json($list);
    }
   //将劳务合同遍历到页面
    public function labcontracteditor($id)
    {
        $list = db('lab_contract')->where('id',$id)->find();
        return view('admin@Contract/editorLabcontract',
            [
                'list'=>$list
            ]);
    }

    //加载增加劳务合同页面
    public function formLabcontract()
    {
        return view('admin@Contract/formLabcontract');
    }

    //添加劳务合同
    public function insertLabcontract(Request $request)
    {

        $data = input('param.');
        if(empty($data['employers'])){
            $this->error('用人单位',url('admin/Contract/formLabcontract'));
        }
        $file = $request->file('contract_details');

        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/');
            if($info){
                // 成功上传后 获取上传信息
//                echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
//                echo $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
//                echo $info->getFilename();
                $data['contract_details'] = '/uploads/'.$info->getSaveName();

                $res =explode('\\', $data['contract_details']);

                $filesa=implode('/',$res);

                $data['contract_details'] =$filesa;

                $reqult = db('lab_contract')->insertGetId($data);

                if ($reqult>0){
                    $this->success('劳务合同添加成功',url('admin/Contract/Labcontract'));
                }else{
                    $this->error('劳务合同添加失败',url('admin/Contract/formLabcontract'));
                }

            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
    }

   //更新劳务合同
    public function addLabcontract($id)
    {
        $data = $_POST;

        $list = db('lab_contract')->where('id',$id)->update($data);
        if ($list){
            $info['status']=true;
        }else{
            $info['status']=false;
        }
        return json($info);
    }
   //删除劳务合同
    public function delectLabcontract($id)
    {
        $result = db('lab_contract')->where(['id'=>$id])->delete();

        if ($result) {
            $info['status'] = true;
        }else{
            $info['status'] = false;
        }
        return  json($info);
    }
    //将机械合同数据遍历到页面上
    public function machine()
    {
        $list = db('machine_contract')->select();
//        var_dump($list);exit;
        return view('admin@Contract/machine',
            ['list'=>$list]
        );
    }
    //将添加机械合同页面加载到页面
    public function formmachine()
    {
        $list= db('mechanical_usage')->select();
        return view('admin@Contract/formmachine',
            ['list'=>$list]
            );
    }
    //添加机械合同
    public function addmachine()
    {

        $data = $_POST;
        $result = db('machine_contract')->insert($data);
        if ($result>0) {
            $info['status'] = true;
        }else{
            $info['status'] = false;
        }
        return  json($info);
    }
    //将一条机械合同数据遍历到页面上
    public function detailmachine($id)
    {
        $list = db('machine_contract')->where('id',$id)->find();
        return  view('admin@Contract/detailmachine',
            ['list'=>$list]
            );
    }
    //将一条机械合同数据遍历到页面上,更改数据使用
    public function  upmachine($id)
    {
        $list = db('machine_contract')->where('id',$id)->find();
        return  view('admin@Contract/editormachine',
            ['list'=>$list]
        );

    }
    //根据id删除一条数据
    public function delectMachine($id)
    {
        $result = db('machine_contract')->where(['id'=>$id])->delete();

        if ($result) {
            $info['status'] = true;
        }else{
            $info['status'] = false;
        }
        return  json($info);
    }
    //更新过的数据存到数据库
    public function editormachine()
    {
        $data = $_POST;
        $result = db('machine_contract')->update($data);
        if ($result>0) {
            $info['status'] = true;
        }else{
            $info['status'] = false;
        }
        return  json($info);
    }


}
